/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Clas;
import vn.webapp.model.Classes;
import vn.webapp.model.Classing;
import vn.webapp.model.Student;

@Repository("classesDAO")
//@SuppressWarnings({"unchecked", "rawtypes"})
public class ClassesDAOImpl extends BaseDao implements ClassesDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public List<Classes> filter(int page, int pageSize) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Classes.class);
            criteria.setMaxResults(pageSize);
            if (page > 0) {
                criteria.setFirstResult(page * pageSize);
            } else {
                criteria.setFirstResult(page);
            }
            List<Classes> classes = criteria.list();
            commit();
            return classes;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Clas> filterTesting(int page, int pageSize)
    {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Clas.class);
            criteria.setMaxResults(pageSize);
            if (page > 0) {
                criteria.setFirstResult(page * pageSize);
            } else {
                criteria.setFirstResult(page);
            }
            List<Clas> classes = criteria.list();
            commit();
            return classes;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Classes> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Classes.class);
           // criteria.setMaxResults(pageSize);
            
            List<Classes> classes = criteria.list();
            commit();
            return classes;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    /**
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Clas> listTesting() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Clas.class);
            List<Clas> clas = criteria.list();
            commit();
            return clas;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Classes viewDetail(String id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Classes c WHERE c.class_id = '" + id + "'");
        return (Classes) q.uniqueResult();
    }

    @Override
    public Classes checkExistsCode(String code, String id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Classes c WHERE c.code = '" + code + "' AND c.class_id != '" + id + "'");
        return (Classes) q.uniqueResult();
    }

    @Override
    public int removeClass(String id) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "DELETE from Classes c WHERE c.class_id ='" + id + "'");
        int result = query.executeUpdate();
        return result;
    }

    @Override
    public int updateTheClass(String code, String id, String name) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "UPDATE Classes SET code ='" + code + "', name = '" + name + "' WHERE class_id = '" + id + "'");
        int result = query.executeUpdate();
        return result;
    }

    @Override
    public int saveAClass(Classes classes) {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(classes);
           commit();
           return id;           
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    @Override
    public int count(){
        //int size = 0;
         try {
            begin();
            Criteria criteria = getSession().createCriteria(Classes.class);            
            List<Classes> classes = criteria.list();
            commit();
            return classes.size();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Classes> getByIds(List<Integer> ids){
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Classes.class, "classes");
            List<Classes> classes = new ArrayList<>();
            criteria.add(Restrictions.in("classes.class_id",ids));
            classes = (List<Classes>) criteria.list();
            commit();
            return classes;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
}
